package com.pigdogbay.anagramsolver;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;
import com.pigdogbay.androidutils.utils.ActivityUtils;

import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class AboutFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels/2;
        Button btn = (Button) rootView.findViewById(R.id.aboutBtnRate);
        btn.setMinimumWidth(width);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showWebPage(R.string.market_anagramsolver);
				trackButtonPress("rate_btn");
			}
		});
        btn = (Button) rootView.findViewById(R.id.aboutBtnFacebook);
        btn.setMinimumWidth(width);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showWebPage(R.string.market_pigdogbay_apps);
				trackButtonPress("more_apps_btn");
			}
		});
        btn = (Button) rootView.findViewById(R.id.aboutBtnLegal);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showLegalNotices();
				trackButtonPress("legal_btn");
			}
		});
        

        return rootView;
    }

    private void showWebPage(int urlId)
	{
		try
		{
			ActivityUtils.ShowWebPage(getActivity(), getActivity().getString(urlId));
		}
		catch (ActivityNotFoundException e)
		{ 
			Toast.makeText(getActivity(), getActivity().getString(R.string.web_error), Toast.LENGTH_LONG)
			.show();
		}
	}
    
    private void showLegalNotices(){
		ActivityUtils.showInfoDialog(getActivity(), R.string.copyright_title, R.string.copyright);
    }
    
    private void trackButtonPress(String action){
    	try{
    	  // May return null if a EasyTracker has not yet been initialized with a
    	  // property ID.
    	  EasyTracker easyTracker = EasyTracker.getInstance(getActivity());
    	  if (easyTracker==null){return;}
    	  // MapBuilder.createEvent().build() returns a Map of event fields and values
    	  // that are set and sent with the hit.
    	  easyTracker.send(MapBuilder
    	      .createEvent("ui_action",     // Event category (required)
    	                   "button_press",  // Event action (required)
    	                   action,   // Event label
    	                   null)            // Event value
    	      .build()
    	  );
    	}catch(Exception e){}
    }
}
